home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Multimedia / Movie3.0 / Source / mpegDecode / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-06  |  10.1 KB  |  453 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. #include <stdlib.h>
  23. #include "video.h"
  24. #include "proto.h"
  25. #include "util.h"
  26.  
  27. /* Declarations of global variables used. */
  28.  
  29. unsigned int curBits;
  30. int bitOffset;
  31. int bufLength;
  32. unsigned int *bitBuffer;
  33.  
  34. /* Bit masks used by bit i/o operations. */
  35.  
  36. unsigned int nBitMask[] = { 0x00000000, 0x80000000, 0xc0000000, 0xe0000000, 
  37.                 0xf0000000, 0xf8000000, 0xfc000000, 0xfe000000, 
  38.                 0xff000000, 0xff800000, 0xffc00000, 0xffe00000, 
  39.                 0xfff00000, 0xfff80000, 0xfffc0000, 0xfffe0000, 
  40.                 0xffff0000, 0xffff8000, 0xffffc000, 0xffffe000, 
  41.                 0xfffff000, 0xfffff800, 0xfffffc00, 0xfffffe00, 
  42.                 0xffffff00, 0xffffff80, 0xffffffc0, 0xffffffe0, 
  43.                 0xfffffff0, 0xfffffff8, 0xfffffffc, 0xfffffffe};
  44.  
  45. unsigned int bitMask[] = {  0xffffffff, 0x7fffffff, 0x3fffffff, 0x1fffffff, 
  46.                 0x0fffffff, 0x07ffffff, 0x03ffffff, 0x01ffffff,
  47.                 0x00ffffff, 0x007fffff, 0x003fffff, 0x001fffff,
  48.                 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff,
  49.                 0x0000ffff, 0x00007fff, 0x00003fff, 0x00001fff,
  50.                 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff,
  51.                 0x000000ff, 0x0000007f, 0x0000003f, 0x0000001f,
  52.                 0x0000000f, 0x00000007, 0x00000003, 0x00000001};
  53.  
  54. unsigned int rBitMask[] = { 0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8, 
  55.                 0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80, 
  56.                 0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800, 
  57.                 0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000, 
  58.                 0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000, 
  59.                 0xfff00000, 0xffe00000, 0xffc00000, 0xff800000, 
  60.                 0xff000000, 0xfe000000, 0xfc000000, 0xf8000000, 
  61.                 0xf0000000, 0xe0000000, 0xc0000000, 0x80000000};
  62.  
  63. unsigned int bitTest[] = {  0x80000000, 0x40000000, 0x20000000, 0x10000000, 
  64.                 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  65.                 0x00800000, 0x00400000, 0x00200000, 0x00100000,
  66.                 0x00080000, 0x00040000, 0x00020000, 0x00010000,
  67.                 0x00008000, 0x00004000, 0x00002000, 0x00001000,
  68.                 0x00000800, 0x00000400, 0x00000200, 0x00000100,
  69.                 0x00000080, 0x00000040, 0x00000020, 0x00000010,
  70.                 0x00000008, 0x00000004, 0x00000002, 0x00000001};
  71.  
  72.  
  73. /*
  74.  *--------------------------------------------------------------
  75.  *
  76.  * correct_underflow --
  77.  *
  78.  *    Called when buffer does not have sufficient data to 
  79.  *      satisfy request for bits.
  80.  *      Calls get_more_data, an application specific routine
  81.  *      required to fill the buffer with more data.
  82.  *
  83.  * Results:
  84.  *      None really.
  85.  *  
  86.  * Side effects:
  87.  *    buf_length and buffer fields in curVidStream structure
  88.  *      may be changed.
  89.  *
  90.  *--------------------------------------------------------------
  91.  */
  92.  
  93. void 
  94. correct_underflow() {
  95.  
  96.   int status;
  97.  
  98.   status = get_more_data(curVidStream->buf_start,
  99.              curVidStream->max_buf_length,
  100.              &bufLength, &bitBuffer);
  101.  
  102.   if (status  < 0) {
  103.     // Unexpected read error.
  104.     exit(1);
  105.   }
  106.   else if ((status == 0) && (bufLength < 1)) {
  107.     // Improper or missing sequence end code.
  108. #ifdef ANALYSIS
  109.     PrintAllStats();
  110. #endif
  111.  
  112.     if (loopFlag) longjmp(env, 1);
  113.     DestroyVidStream(curVidStream);
  114.     exit(0);
  115.   }
  116. #ifdef UTIL2
  117.   curBits = *bitBuffer << bitOffset;
  118. #else
  119.   curBits = *bitBuffer;
  120. #endif
  121.  
  122. }
  123.  
  124.  
  125. /*
  126.  *--------------------------------------------------------------
  127.  *
  128.  * next_bits --
  129.  *
  130.  *    Compares next num bits to low order position in mask.
  131.  *      Buffer pointer is NOT advanced.
  132.  *
  133.  * Results:
  134.  *    TRUE, FALSE, or error code.
  135.  *
  136.  * Side effects:
  137.  *    None.
  138.  *
  139.  *--------------------------------------------------------------
  140.  */
  141.  
  142. int next_bits(num, mask)
  143. int num;
  144. unsigned int mask;
  145. {
  146.   unsigned int stream;
  147.   int ret_value;
  148.  
  149.   /* If no current stream, return error. */
  150.  
  151.   if (curVidStream == NULL)
  152.     return NO_VID_STREAM;
  153.  
  154.   /* Get next num bits, no buffer pointer advance. */
  155.  
  156.   show_bitsn(num, stream);
  157.  
  158.   /* Compare bit stream and mask. Set return value toTRUE if equal, FALSE if
  159.      differs. 
  160.   */
  161.  
  162.   if (mask == stream) {
  163.     ret_value = TRUE;
  164.   } else ret_value = FALSE;
  165.  
  166.   /* Return return value. */
  167.  
  168.   return ret_value;
  169. }
  170.  
  171.  
  172. /*
  173.  *--------------------------------------------------------------
  174.  *
  175.  * get_ext_data --
  176.  *
  177.  *    Assumes that bit stream is at begining of extension
  178.  *      data. Parses off extension data into dynamically 
  179.  *      allocated space until start code is hit. 
  180.  *
  181.  * Results:
  182.  *    Pointer to dynamically allocated memory containing
  183.  *      extension data.
  184.  *
  185.  * Side effects:
  186.  *    Bit stream irreversibly parsed.
  187.  *
  188.  *--------------------------------------------------------------
  189.  */
  190.  
  191. char *get_ext_data ()
  192. {
  193.   int size, marker;
  194.   char *dataPtr;
  195.   unsigned int data;
  196.  
  197.   /* Set initial ext data buffer size. */
  198.  
  199.   size = EXT_BUF_SIZE;
  200.  
  201.   /* Allocate ext data buffer. */
  202.  
  203.   dataPtr = (char *) malloc(size);
  204.  
  205.   /* Initialize marker to keep place in ext data buffer. */
  206.  
  207.   marker = 0;
  208.  
  209.   /* While next data is not start code... */
  210.   while (!next_bits(24, 0x000001)) {
  211.  
  212.     /* Get next byte of ext data. */
  213.  
  214.     get_bits8(data);
  215.  
  216.     /* Put ext data into ext data buffer. Advance marker. */
  217.  
  218.     dataPtr[marker] = (char) data;
  219.     marker++;
  220.  
  221.     /* If end of ext data buffer reached, resize data buffer. */
  222.  
  223.     if (marker == size) {
  224.       size += EXT_BUF_SIZE;
  225.       dataPtr = (char *) realloc(dataPtr, size);
  226.     }
  227.   }
  228.  
  229.   /* Realloc data buffer to free any extra space. */
  230.  
  231.   dataPtr = (char *) realloc(dataPtr, marker);
  232.  
  233.   /* Return pointer to ext data buffer. */
  234.  
  235.   return dataPtr;
  236. }
  237.  
  238.  
  239. /*
  240.  *--------------------------------------------------------------
  241.  *
  242.  * next_start_code --
  243.  *
  244.  *    Parses off bitstream until start code reached. When done
  245.  *      next 4 bytes of bitstream will be start code. Bit offset
  246.  *      reset to 0.
  247.  *
  248.  * Results:
  249.  *    Status code.
  250.  *
  251.  * Side effects:
  252.  *    Bit stream irreversibly parsed.
  253.  *
  254.  *--------------------------------------------------------------
  255.  */
  256.  
  257. int next_start_code()
  258. {
  259.   int state;
  260.   int byteoff;
  261.   unsigned int data;
  262.  
  263.   /* If no current stream, return error. */
  264.  
  265.   if (curVidStream == NULL)
  266.     return NO_VID_STREAM;
  267.  
  268.   /* If insufficient buffer length, correct underflow. */
  269.  
  270.   if (bufLength < 2) {
  271.     correct_underflow();
  272.   }
  273.  
  274.   /* If bit offset not zero, reset and advance buffer pointer. */
  275.  
  276.   byteoff = bitOffset % 8;
  277.  
  278.   if (byteoff != 0) {
  279.     flush_bits((8-byteoff));
  280.   }
  281.  
  282.   /* Set state = 0. */
  283.  
  284.   state = 0;
  285.  
  286.   /* While buffer has data ... */
  287.  
  288.   while(bufLength > 0) {
  289.  
  290.     /* If insufficient data exists, correct underflow. */
  291.  
  292.     if (bufLength < 2) {
  293.       correct_underflow();
  294.     }
  295.  
  296.     /* If next byte is zero... */
  297.  
  298.     get_bits8(data);
  299.  
  300.     if (data == 0) {
  301.  
  302.       /* If state < 2, advance state. */
  303.  
  304.       if (state < 2) state++;
  305.     }
  306.  
  307.     /* If next byte is one... */
  308.  
  309.     else if (data == 1) {
  310.  
  311.       /* If state == 2, advance state (i.e. start code found). */
  312.  
  313.       if (state == 2) state++;
  314.  
  315.       /* Otherwise, reset state to zero. */
  316.  
  317.       else state = 0;
  318.     }
  319.  
  320.     /* Otherwise byte is neither 1 or 0, reset state to 0. */
  321.  
  322.     else {
  323.       state = 0;
  324.     }
  325.  
  326.     /* If state == 3 (i.e. start code found)... */
  327.  
  328.     if (state == 3) {
  329.  
  330.       /* Set buffer pointer back and reset length & bit offsets so
  331.      next bytes will be beginning of start code. 
  332.       */
  333.  
  334.       bitOffset = bitOffset - 24;
  335.  
  336. #ifdef ANALYSIS
  337.       bitCount -= 24;
  338. #endif
  339.  
  340.       if (bitOffset < 0) {
  341.     bitOffset = 32 + bitOffset;
  342.     bufLength++;
  343.     bitBuffer--;
  344. #ifdef UTIL2
  345.     curBits = *bitBuffer << bitOffset;
  346. #else
  347.     curBits = *bitBuffer;
  348. #endif
  349.       }
  350.       else {
  351. #ifdef UTIL2
  352.     curBits = *bitBuffer << bitOffset;
  353. #else
  354.     curBits = *bitBuffer;
  355. #endif
  356.       }
  357.  
  358.       /* Return success. */
  359.  
  360.       return OK;
  361.     }
  362.   }
  363.  
  364.   /* Return underflow error. */
  365.  
  366.   return UNDERFLOW;
  367. }
  368.  
  369.  
  370. /*
  371.  *--------------------------------------------------------------
  372.  *
  373.  * get_extra_bit_info --
  374.  *
  375.  *    Parses off extra bit info stream into dynamically 
  376.  *      allocated memory. Extra bit info is indicated by
  377.  *      a flag bit set to 1, followed by 8 bits of data.
  378.  *      This continues until the flag bit is zero. Assumes
  379.  *      that bit stream set to first flag bit in extra
  380.  *      bit info stream.
  381.  *
  382.  * Results:
  383.  *    Pointer to dynamically allocated memory with extra
  384.  *      bit info in it. Flag bits are NOT included.
  385.  *
  386.  * Side effects:
  387.  *    Bit stream irreversibly parsed.
  388.  *
  389.  *--------------------------------------------------------------
  390.  */
  391.  
  392. char *get_extra_bit_info ()
  393. {
  394.   int size, marker;
  395.   char *dataPtr;
  396.   unsigned int data;
  397.  
  398.   /* Get first flag bit. */
  399.   get_bits1(data);
  400.  
  401.   /* If flag is false, return NULL pointer (i.e. no extra bit info). */
  402.  
  403.   if (!data) return NULL;
  404.  
  405.   /* Initialize size of extra bit info buffer and allocate. */
  406.  
  407.   size = EXT_BUF_SIZE;
  408.   dataPtr = (char *) malloc(size);
  409.  
  410.   /* Reset marker to hold place in buffer. */
  411.  
  412.   marker = 0;
  413.  
  414.   /* While flag bit is true. */
  415.  
  416.   while (data) {
  417.  
  418.     /* Get next 8 bits of data. */
  419.     get_bits8(data);
  420.  
  421.     /* Place in extra bit info buffer. */
  422.  
  423.     dataPtr[marker] = (char) data;
  424.     marker++;
  425.  
  426.     /* If buffer is full, reallocate. */
  427.  
  428.     if (marker == size) {
  429.       size += EXT_BUF_SIZE;
  430.       dataPtr = (char *) realloc(dataPtr, size);
  431.     }
  432.  
  433.     /* Get next flag bit. */
  434.     get_bits1(data);
  435.   }
  436.  
  437.   /* Reallocate buffer to free extra space. */
  438.  
  439.   dataPtr = (char *) realloc(dataPtr, marker);
  440.  
  441.   /* Return pointer to extra bit info buffer. */
  442.  
  443.   return dataPtr;
  444. }
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.